﻿using System;
using System.Windows;
using MyFloridaHouse.ServiceConn.DistrBldrSvcRef;
using MyFloridaHouse.libSvOpenDistricts.Criteria;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using MyFloridaHouse.libSvOpenDistricts.MapShapes;

namespace MyFloridaHouse.libSvOpenDistricts.DataService
{
    public class PendingGetRegionListDataRequest
    {
        public int layerID;
        public List<int> regionList;

        public PendingGetRegionListDataRequest(int layerID, List<int> regionList)
        {
            this.layerID = layerID;
            this.regionList = regionList;
        }
    }

    public delegate void ServiceDALGetRegionListDataCompletedEventHandler(LayerRegionsDataSTO args);

    public partial class ServiceDAL
    {
        public static event ServiceDALGetRegionListDataCompletedEventHandler GetRegionListDataCompleted;


        private static void RaiseGetRegionListDataCompleted(object sender, LayerRegionsDataSTO sto)
        {
            if (GetRegionListDataCompleted != null)
            {
                GetRegionListDataCompleted(sto);
            }
            else
            {
                // todo: Log this error
                throw (new NotImplementedException("No Subscribers to GetRegionListDataCompleted Event"));
            }
        }

        private int _regionListDataRequestActiveCount = 0;
        public int regionListDataRequestActiveCount
        {
            set
            {
                if (value != _regionListDataRequestActiveCount)
                {
                    _regionListDataRequestActiveCount = value;
                    NotifyPropertyChanged("regionListDataRequestActiveCount");
                }
            }
            get { return _regionListDataRequestActiveCount; }
        }


        private PendingGetRegionListDataRequest pendingGetRegionListDataRequest = null;

        public void GetRegionListData(int layerID, List<int> regionIDList)
        {
            try
            {
                if (regionListDataRequestActiveCount > 0)
                {
                    // set this incoming request as the next request, forget about a prior request
                    pendingGetRegionListDataRequest = new PendingGetRegionListDataRequest(layerID, regionIDList);
                }
                else
                {
                    DataDictionaryIDList dictionaryIDstruct = CriteriaDatasetManager.GetActiveDictionaryIDs();

                    ObservableCollection<int> newRegionIDList = new ObservableCollection<int>();
                    foreach (var rgn in regionIDList)
                    {
                        newRegionIDList.Add(rgn);
                    }

                    var proxy = new DistrBldrClient(_relativeBindingName);
                    proxy.GetRegionListDataCompleted += ServiceGetRegionListDataCompleted;
                    proxy.GetRegionListDataAsync(layerID, newRegionIDList, dictionaryIDstruct.layerIDList, dictionaryIDstruct.dictionaryIDList);
                    regionListDataRequestActiveCount++;
                }
            }
            catch (Exception ex)
            {
                regionListDataRequestActiveCount--;
                MessageBox.Show(ex.Message);
            }

        }

        private void ServiceGetRegionListDataCompleted(object sender, GetRegionListDataCompletedEventArgs args)
        {
            regionListDataRequestActiveCount--;
            try
            {
                if (!IsError("GetLayerRegionsDataInViewCompleted", args.Error, args.Result, args.Result.errorMessage))
                {
                    RaiseGetRegionListDataCompleted(this, args.Result);
                    if (pendingGetRegionListDataRequest != null)
                    {
                        GetRegionListData(pendingGetRegionListDataRequest.layerID, pendingGetRegionListDataRequest.regionList);
                        pendingGetRegionListDataRequest = null;
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
